package dk.ihk.shopular.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContextUtils;

import dk.ihk.shopular.models.User;
import dk.ihk.shopular.services.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;

	@RequestMapping("/dashboard")
	public String showWelcome(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		if (!userService.validAuthentication(request)) return "loginFailed";
		
		return "dashboard";
	}
	
	@RequestMapping("/user/login")
	public String showLoginDialog(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		return "login";
	}
	
	@RequestMapping("/user/logout")
	public String logoutUser(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		request.getSession().setAttribute("user", "");
		request.getSession().setAttribute("pass", "");
		request.getSession().invalidate();
		return "login";
	}
	
	@RequestMapping("/user/verifyLogin")
	public String loginUser(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		request.getSession().removeAttribute("user");
		request.getSession().removeAttribute("pass");
		
		String user = request.getParameter("username");
		String password = userService.hash(request.getParameter("password"));
		
		if (userService.verifyLogin(user, password)) { //MD5, SHA1?
			//cool! valid login data
			request.getSession().setAttribute("user", user);
			request.getSession().setAttribute("pass", password);
			
			String redirectURL = "/shopular/dashboard.do";
		    try {
				response.sendRedirect(redirectURL);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "dashboard";
		} else 
			return "loginFailed";
	}
	
	@RequestMapping("/users/list")
	public String listAllUsers(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		if (!userService.validAuthentication(request)) return "loginFailed";
		
		List<User> userList = userService.getUserPool();
		request.setAttribute("users", userList);
		
		return "listUsers";
	}
	
	@RequestMapping("/user/myFriends")
	public String showMyFriends(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		if (!userService.validAuthentication(request)) return "loginFailed";
		
		List<User> userList = userService.getCurrentUser(request).getFriends();
		request.setAttribute("users", userList);
		
		return "friendList";
	}
	
	@RequestMapping("/user/add")
	public String addAsFriend(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		//retrieve new friend and add his/her to the friendlist of the current user
		String user = request.getParameter("username");
		userService.getCurrentUser(request).addAsFriend(userService.findUserByName(user));
		
		//now load the new friendlist and pass it to the template
		List<User> userList = userService.getCurrentUser(request).getFriends();
		request.setAttribute("users", userList);
		
		return "friendList";
	}
	
	
	//some getters and setters
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
